<template>
    <div class="form">
        <el-form label-width="auto" :rules="formRules" ref="formRef" :model="publishInformation">
            <el-form-item label="类型">
                <el-select v-model="publishInformation.announcementType" placeholder="公告类型" style="width: 400px">
                    <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
                </el-select>
            </el-form-item>
            <el-form-item label="标题" prop="rubric">
                <el-input type="tel" v-model="publishInformation.rubric" placeholder="请输入"></el-input>
            </el-form-item>
            <el-form-item lable="开始时间">
                <div class="block">
                    <span class="demonstration">开始时间</span>
                    <el-date-picker v-model="publishInformation.startTime" type="date" placeholder="Pick a day" :size="size" />
                </div>
            </el-form-item>
            <el-form-item lable="结束时间">
                <div class="block">
                    <span class="demonstration">结束时间</span>
                    <el-date-picker v-model="publishInformation.endTime" type="date" placeholder="Pick a day" :size="size" />
                </div>
            </el-form-item>
            <el-form-item label="公告内容">
                <el-input type="textarea" v-model="publishInformation.notes" style="width: 400px" :rows="6"
                    placeholder="请输入备注"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="save(formRef)">保存</el-button>
                <el-button type="info" @click="cancel">取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script setup>
import http from '../utils/http'
import { ref } from 'vue'
import { ElMessageBox } from 'element-plus'
import { useRouter } from 'vue-router';
import { onBeforeMount } from 'vue';
let router = useRouter()
const size = ref < 'default' | 'large' | 'small' > ('default')
const createTime = ref('')
const options = [
    {
        value: '业主通知',
        label: '业主通知',
    },
    {
        value: '员工通知',
        label: '员工通知',
    },
    {
        value: '业主微信通知',
        label: '业主微信通知',
    }
]
let publishInformation = ref({
    id: router.currentRoute.value.query.id,
    announcementType: '',
    rubric: '',
    startTime: '',
    endTime: '',
    notes: ''
})
let formRef = ref(null) //表单dom对象
let formRules = {
    rubric: [
        { required: true, message: '请输入标题', trigger: 'blur' }
    ]
}
onBeforeMount(async () => {
    if (publishInformation.value.id) {
        let resp = await http.get(`/publishInformation/get/${publishInformation.value.id}`)
        publishInformation.value = resp.data
    }
})
async function save(formRef) {
    formRef.validate(async function (valid) {
        if (valid) {
            let path = publishInformation.value.id ? '/publishInformation/update' : '/publishInformation/add'
            let tip = publishInformation.value.id ? '修改' : '保存'
            let resp = await http.post(path, publishInformation.value)
            if (resp.code == 200) {
                ElMessageBox.alert(tip + '账户成功', tip + '账户', {
                    confirmButtonText: 'OK'
                })
                router.go(-1)
            } else {

            }
        }
    })
}
function cancel() {
    router.go(-1)
}

</script>
<style lang="css" scoped>
.form {
    width: 400px;
    margin: 0 auto;
}

.el-form-item__label {
    width: 120px;
}
</style>